package com.feidian.helper.ui.fragment;

import java.io.File;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.feidian.helper.R;
import com.feidian.helper.bean.Table;
import com.feidian.helper.bean.User;
import com.feidian.helper.ui.LoginActivity;
import com.feidian.helper.ui.PersonalActivity;
import com.feidian.helper.ui.SettingActivity;
import com.feidian.helper.ui.widget.RatingBar;
import com.feidian.helper.util.Utils;

/**
 * 
 * @author WooYou and linxingqianglai
 * @category
 * @category 个人QQ 2.头像 3.接单信用 4.发单信用 5.重置密码 6.登录 7.退出
 * 
 */
public class NewSettingFragment extends Fragment implements
		View.OnClickListener {
	private final static int LOGIN_STATE=0X123;
	private ImageView right;
	private ViewGroup mPersonal;
	private ViewGroup mQQNumber;
	private ViewGroup mPhoneNumber;
	private ViewGroup mJiedan;
	private ViewGroup mFandan;

	private ViewGroup mLogin;
	private ViewGroup mLogout;

	// mNameTV 名字
	private TextView mNameTV;
	// qq
	private TextView mQQTV;
	// 电话
	private TextView mPhoneTV;
	// 发单信用
	private RatingBar mjdRB;
	// 接单信用
	private RatingBar mfdRB;
	com.feidian.helper.ui.view.CircleImageView head;
	SharedPreferences sp;
	Handler handler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case LOGIN_STATE:
				upData();
				break;

			default:
				break;
			}
		};
	};

	@Override
	public View onCreateView(LayoutInflater inflater,
			@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
		sp = getActivity().getSharedPreferences(Table.CommentUserInf,
				Context.MODE_PRIVATE);
		View view = inflater.inflate(R.layout.new_setting_fragment, container,
				false);
		TextView title = (TextView) view.findViewById(R.id.title);
		title.setText(getActivity().getString(R.string.setting));
		view.findViewById(R.id.back).setVisibility(View.GONE);
		right = (ImageView) view.findViewById(R.id.right);
		right.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				// 跳转至设置界面
				startActivity(new Intent(getActivity(), SettingActivity.class));
			}
		});
		// 个人头像设置
		mPersonal = (ViewGroup) view.findViewById(R.id.ll_personal);
		mPersonal.setOnClickListener(this);

		// QQ账号
		mQQNumber = (ViewGroup) view.findViewById(R.id.ll_qqnumber);
		mQQNumber.setOnClickListener(this);

		// 手机号
		mPhoneNumber = (ViewGroup) view.findViewById(R.id.ll_phonenumber);
		mPhoneNumber.setOnClickListener(this);

		// 接单信用
		mJiedan = (ViewGroup) view.findViewById(R.id.ll_jd);
		mJiedan.setOnClickListener(this);
		// 设置接单信用星级
		mjdRB = (RatingBar) view.findViewById(R.id.rb_jd);
		mjdRB.setStar(5);

		// 发单信用
		mFandan = (ViewGroup) view.findViewById(R.id.ll_fd);
		mFandan.setOnClickListener(this);
		// 设置发单信用星级
		mfdRB = (RatingBar) view.findViewById(R.id.rb_fd);
		mfdRB.setStar(5);

		// 跳到登录界面
		mLogin = (ViewGroup) view.findViewById(R.id.login);
		mLogin.setOnClickListener(this);
		// 退出登录
		mLogout = (ViewGroup) view.findViewById(R.id.logout);
		mLogout.setOnClickListener(this);
		head = (com.feidian.helper.ui.view.CircleImageView) view
				.findViewById(R.id.head);

		mNameTV = (TextView) view.findViewById(R.id.name_tv);

		mQQTV = (TextView) view.findViewById(R.id.qq_tv);

		mPhoneTV = (TextView) view.findViewById(R.id.phone_tv);
		upData();
		return view;
	}

	private void upData() {
		//若用户未登录
		//if (User.isLogin == false){
		if(!sp.getBoolean(Table.isLogin, false)) {
			head.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.women));
		    mNameTV.setText("沸腾");
		    mQQTV.setText("暂无");
		    mPhoneTV.setText("暂无");
		    mLogin.setVisibility(View.VISIBLE);
			mLogout.setVisibility(View.GONE);
		    return;
		}
		
		if (User.head == null) {
			Log.e("head=null", "...");
			head.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.women));
		}
		if (User.head != null) {
			head.setImageBitmap(User.head);
		} else {
			String tem=sp.getString(Table.headHeadImageName, "-1");
			Log.e("tem==="+tem,"................");
			if (!tem.equals("-1")&&!tem.equals("")&&!tem.equals("null")) {
				File file = new File(Utils.getHelperFile(),tem);
				Bitmap bitmap = BitmapFactory.decodeFile(file.toString());
				if (bitmap != null) {
					User.head = bitmap;
					head.setImageBitmap(User.head);
					Log.e("bitmap!=null",".................");
				}
			}
		}
		if (User.name != null && !User.name.equals("null")) {
			mNameTV.setText(User.name);
			if (!sp.getString(Table.name, "-1").equals("-1")) {
				mNameTV.setText(sp.getString(Table.name, "-1"));
				if (!sp.getString(Table.qq, "-1").equals("-1")&&!sp.getString(Table.qq, "-1").equals("null")) {
					User.qq = sp.getString(Table.qq, "-1");
					mQQTV.setText(User.qq);
				}
				else {
					User.qq = sp.getString(Table.qq, "-1");
					mQQTV.setText("无");
				}

				if (!sp.getString(Table.phone, "-1").equals("-1")&&!sp.getString(Table.phone, "-1").equals("无")) {
					User.phone = sp.getString(Table.phone, "-1");
					mPhoneTV.setText(User.phone);
				}else{
					User.phone = sp.getString(Table.phone, "-1");
					mPhoneTV.setText("无");
				}
			}
		} else {
			Log.e("User.name==null", "..................");
			if (!sp.getString(Table.name, "-1").equals("-1")) {
				mNameTV.setText(sp.getString(Table.name, "-1"));
				if (!sp.getString(Table.qq, "-1").equals("-1")&&!sp.getString(Table.qq, "-1").equals("null")) {
					User.qq = sp.getString(Table.qq, "-1");
					mQQTV.setText(User.qq);
				}
				else {
					User.qq = sp.getString(Table.qq, "-1");
					mQQTV.setText("无");
				}

				if (!sp.getString(Table.phone, "-1").equals("-1")&&!sp.getString(Table.phone, "-1").equals("null")) {
					User.phone = sp.getString(Table.phone, "-1");
					mPhoneTV.setText(User.phone);
				}else{
					User.phone = sp.getString(Table.phone, "-1");
					mPhoneTV.setText("无");
				}
			}
		}
		//if (User.isLogin) {
		if(sp.getBoolean(Table.isLogin, false)) {
			Log.e("User.isLogin true","<<<<<<<<<<<<<<<<<<<<<<<<<<");
			
			mLogin.setVisibility(View.GONE);
			mLogout.setVisibility(View.VISIBLE);
		} else {
			Log.e("User.isLogin false","<<<<<<<<<<<<<<<<<<<<<<<<<<");
			mLogin.setVisibility(View.VISIBLE);
			mLogout.setVisibility(View.GONE);
		}
	}

	@Override
	public void onStart() {
		if (User.head != null && head != null) {
			head.setImageBitmap(User.head);
		}
		upData();
		super.onStart();
	}

	@Override
	public void onResume() {
		super.onResume();
		upData();
	}

	@Override
	public void onClick(View v) {
		int id = v.getId();
		switch (id) {
		case R.id.ll_personal:
			startActivity(new Intent(getActivity(), PersonalActivity.class));
			break;
		case R.id.logout:
			showLogoutDialog();
			break;
		case R.id.ll_qqnumber:
			startActivity(new Intent(getActivity(), PersonalActivity.class));
			break;
		case R.id.ll_phonenumber:
			startActivity(new Intent(getActivity(), PersonalActivity.class));
			break;
		case R.id.login:
			startActivity(new Intent(getActivity(), LoginActivity.class));
		default:
			break;
		}
	}

	private void showLogoutDialog() {

		final Dialog mDialog = new Dialog(getActivity(), R.style.dialog);
		LayoutInflater layoutInflater = getActivity().getLayoutInflater();
		View view = layoutInflater.inflate(R.layout.logout_dialog, null, false);
		TextView content = (TextView) view.findViewById(R.id.content);
		content.setText("确定要退出登录?");
		Button mSure = (Button) view.findViewById(R.id.sure);
		Button mCancel = (Button) view.findViewById(R.id.no);
		mSure.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				sp = getActivity().getSharedPreferences(Table.CommentUserInf,
						getActivity().MODE_PRIVATE);
				sp.edit().putString(Table.password, "").commit();
				User.password = "null";
				//User.isLogin = false;
				sp.edit().putBoolean("isLogin", false).commit();
				Message msg=handler.obtainMessage();
				msg.what=LOGIN_STATE;
				
				mDialog.dismiss();
				handler.sendMessage(msg);
			}
		});
		mCancel.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				mDialog.dismiss();
			}
		});
		mDialog.setContentView(view);
		mDialog.show();

	}

}
